<!---总览数据页面-->
<template>
  <div class="ele-body ele-body-card">
    <a-row>
      <a-col :span="12" class="block-space-right">
        <a-card title="设备信息" class="card-width">
          <a-descriptions bordered :column="1">
            <a-descriptions-item label="牛号">{{ data.cattleNo }}</a-descriptions-item>
            <a-descriptions-item label="耳标号1">{{ data.earOne }}</a-descriptions-item>
            <a-descriptions-item label="耳标号2">{{ data.earTwo }}</a-descriptions-item>
          </a-descriptions>
        </a-card>
      </a-col>
      <a-col :span="12" class="block-space-right">
        <!-- 右边统计快 -->
        <a-card title="实时数据" class="card-width">
          <a-row :gutter="8">
            <a-col :span="8">
              <a-card>
                <a-statistic
                  title="躺卧状态"
                  :value="cattleData.lieTypeText"
                  :precision="0"
                  suffix=""
                  class="demo-class"
                  :value-style="{ color: '#cf1322' }"
                />
              </a-card>
            </a-col>
            <a-col :span="8">
              <a-card>
                <a-statistic
                  title="心跳速率(次/min)"
                  :value="cattleData.heartRate"
                  :precision="0"
                  suffix=""
                  class="demo-class"
                  :value-style="{ color: '#cf1322' }"
                />
              </a-card>
            </a-col>
            <a-col :span="8">
              <a-card>
                <a-statistic
                  title="速率状态"
                  :value="cattleData.rateStateText"
                  :precision="0"
                  suffix=""
                  class="demo-class"
                  :value-style="{ color: '#cf1322' }"
                />
              </a-card>
            </a-col>
          </a-row>

          <a-row :gutter="8">
            <a-col :span="8">
              <a-card>
                <a-statistic
                  title="昨天的总躺卧时间"
                  :value="cattleData.yesterdaylieTime"
                  :precision="0"
                  suffix=""
                  class="demo-class"
                  :value-style="{ color: '#cf1322' }"
                />
              </a-card>
            </a-col>
            <a-col :span="8">
              <a-card>
                <a-statistic
                  title="昨日躺卧率"
                  :value="cattleData.yesterdayLieRate"
                  :precision="2"
                  suffix=""
                  class="demo-class"
                  :value-style="{ color: '#cf1322' }"
                />
              </a-card>
            </a-col>
            <a-col :span="8">
              <a-card>
                <a-statistic
                  title="昨天总运动量"
                  :value="cattleData.yesterdayStandStep"
                  :precision="0"
                  suffix=""
                  class="demo-class"
                  :value-style="{ color: '#cf1322' }"
                />
              </a-card>
            </a-col>
          </a-row>
        </a-card>
      </a-col>
    </a-row>

  </div>
</template>

<script>
import {CattleRealtimeDataApi} from '@/api/property/cattlemanager/CattleRealtimeDataApi';
import {DeviceLiedownTimesApi} from "@/api/property/cattlesports/DeviceLiedownTimesApi";
import moment from "moment";

export default {
  name: 'CattleManagerCattleLiedownMonitor',
  components: {

  },
  props: {
    data: Object
  },
  data() {
    return {
      cattleData: {},
      loadDetailTimer: null,
    };
  },
  mounted() {
    this.getDetail();
    this.loadDetailTimer = setInterval(this.getDetail, 30000);
  },
  beforeUnmount() {
    clearInterval(this.loadDetailTimer);
  },
  methods: {
    timeChangeFenToTimeStr(minutes) {
      if (minutes == null) {
        return 0;
      } else if (minutes <= 60){
        return minutes + "分钟";
      } else {
        let hours = Math.floor(minutes / 60);
        let remainingMinutes = minutes % 60;
        let str = hours + "小时";
        if (remainingMinutes > 0) {
          str = str + remainingMinutes + "分";
        }
        return str;
      }
    },
    getDetail() {
      let that = this;

      let today = moment().format('YYYY-MM-DD');
      let todayYYYYMMDD = moment().format('YYYYMMDD');
      let yesterday = moment().subtract(1, 'days').format('YYYY-MM-DD');
      let yesterdayYYYYMMDD = moment().subtract(1, 'days').format('YYYYMMDD');
      that.cattleData.todaylieTime = 0;
      that.cattleData.todayStandStep = 0;
      that.cattleData.yesterdaylieTime = 0;
      that.cattleData.yesterdayStandStep = 0;
      that.cattleData.yesterdayLieRate = 0;
      that.cattleData.todayLieRate = 0;
      Promise.all([
        CattleRealtimeDataApi.detail({"cattleId": this.data.cattleId}),
        DeviceLiedownTimesApi.list({
          "cattleId" :  this.data.cattleId,
          "searchBeginTime" : yesterday,
          "searchEndTime" : today,
        })
      ]).then(function (resArr){
        that.cattleData = resArr[0];
        if (that.cattleData.lieType == 'liedown') {
          that.cattleData.lieTypeText = "躺卧";
        } else if (that.cattleData.lieType == 'stand') {
          that.cattleData.lieTypeText = "站立";
        } else {
          that.cattleData.lieTypeText = "未知";
        }
        if (that.cattleData.rateState == 0) {
          that.cattleData.rateStateText = "无目标";
        } else if (that.cattleData.rateState == 1) {
          that.cattleData.rateStateText = "信号微弱";
        } else if (that.cattleData.rateState == 2) {
          that.cattleData.rateStateText = "呼吸正常";
        } else if (that.cattleData.rateState == 3) {
          that.cattleData.rateStateText = "异常移动";
        }
        let todayYesterdayResult = resArr[1];
        let todayYesterdayData = todayYesterdayResult.data || [];
        if (todayYesterdayData.length > 0) {
          for (let i = 0; i < todayYesterdayData.length; i++) {
            if (todayYesterdayData[i].calDate == todayYYYYMMDD) {
              console.log(todayYesterdayData[i]);
              that.cattleData.todaylieTime = that.timeChangeFenToTimeStr(todayYesterdayData[i].lieTime);
              that.cattleData.todayStandStep = todayYesterdayData[i].standStep;
              that.cattleData.todayLieRate = todayYesterdayData[i].lieRate;
            } else if (todayYesterdayData[i].calDate == yesterdayYYYYMMDD) {
              that.cattleData.yesterdaylieTime = that.timeChangeFenToTimeStr(todayYesterdayData[i].lieTime);
              that.cattleData.yesterdayStandStep = todayYesterdayData[i].standStep;
              that.cattleData.yesterdayLieRate = todayYesterdayData[i].lieRate;
            }
          }
        }
        console.log(that.cattleData);
      });

    },


  }
};
</script>
<style scoped>
.block-space-top {
  /*上右下左*/
  padding: 10px 0 0 0;
}

.block-space-right {
  padding: 0 5px 0 0;
}

.block-space-left {
  padding: 0 0 0 5px;
}

.card-width {
  width: 100%;
}

@media screen and (max-width: 880px) {
  .analysis-tabs-extra {
    display: none;
  }
}
</style>
